<?php
App::uses('BootstrapHtmlHelper', 'Plugin/TwitterBootstrap/View/Helper');
class AppHtmlHelper extends BootstrapHtmlHelper {
	const ICON_EXTERNAL_LINK = "external-link";
	const TARGET_BLANK = "_BLANK";

	/**
	 * The path to the events calendar javascript file (inside webroot/js).
	 *
	 * @var String
	 */
	const EVENTS_CALENDAR_PATH = "/dynamic/events-calendar.js.php";
	const EVENTS_CALENDAR_TEMPLATES_PATH = "/bootstrap_calendar/tmpls";

	public function externalLink($title, $url = null, $options = array(), $confirmMessage = false) {
		$default = array('escape' => false, 'title' => "Abre em uma nova janela");
		$options = array_merge($default, (array)$options);

		if ($title) {
			// Insert a blankspace between the word and the icon,
			$title .= ' ';
		}

		$externalLink = parent::tag('i', '', array('class' => parent::ICON_PREFIX.$this->ICON_EXTERNAL_LINK));
		$options['target'] = $this->TARGET_BLANK;

		return parent::link($title.$externalLink, $url, $options, $confirmMessage);
	}

	/**
	 * Loads the events calendar
	 *
	 * @param array $options
	 * @return the HTML <script> tag
	 */
	public function eventsCalendar($calendarId, $options = array()) {
		$default = array('src' => null, 'type' => 'text/javascript');
		$options = array_merge($default, (array)$options);

		if (isset($options['eventsUrl'])) {
			$eventsUrl = $options['eventsUrl'];
			unset($options['eventsUrl']);
		} else {
			// The default url
			$eventsUrl = Router::url(array('controller' => 'eventos', 'action' => 'listarEventosJson'));
		}

		$scriptLink = '%1$sjs%2$s?templatePath=%3$s&eventsUrl=%4$s';
		$scriptLink = sprintf($scriptLink,
				$this->webroot,
				self::EVENTS_CALENDAR_PATH,
				$this->webroot."js".self::EVENTS_CALENDAR_TEMPLATES_PATH."/",
				$eventsUrl);

		// Get the optional eventId
		if (isset($options['eventId'])) {
			$scriptLink .= '&eventId='.$options['eventId'];
			unset($options['eventId']);
		}

		$scriptLink .= '&calendarId='.$calendarId;
		
		$options['src'] = $scriptLink;

		// Automatically inserts the language file
		$this->script('bootstrap_calendar/language/pt-BR', array('block' => 'script'));
		return parent::tag('script', '', $options);
	}
}
?>